package com.cbts.service.person.impl;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter.DEFAULT;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.cbts.dao.UserInfoDao;
import com.cbts.entity.UserInfo;
import com.cbts.service.person.PersonalSettingService;
import com.cbts.util.DES3;
import com.cbts.util.MD5Util;
import com.cbts.util.MailSender;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

public class PersonalSettingServiceImpl implements PersonalSettingService{

	private UserInfoDao userInfoDao;//与员工管理相关的数据访问
	public UserInfoDao getUserInfoDao() {
		return userInfoDao;
	}
	public void setUserInfoDao(UserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}
	//修改密码
	public String updatePassword(UserInfo userInfo, String newPassword,String confirmedPassword) {
		
		//System.out.println("new:"+newPassword+" "+"con:"+confirmedPassword);
		UserInfo ui = userInfoDao.get(userInfo.getUserid());//获取当前用户
		
		if(!MD5Util.validate(userInfo.getPwd(), ui.getPwd()))//输入的密码是否正确
			return "密码错误";
		if(!newPassword.equals(confirmedPassword))//新旧密码是否一致
			return "新密码与确认密码不一致";	
		ui.setPwd(MD5Util.encode2hex(newPassword));	//设置新密码
		userInfoDao.update(ui);//更新密码
		return "密码修改成功";
	}
	//修改密码
		public String updatePassword(String userid, String newPassword,String confirmedPassword) {
			
			//System.out.println("new:"+newPassword+" "+"con:"+confirmedPassword);
			UserInfo ui = userInfoDao.get(userid);//获取当前用户
			
			if(!newPassword.equals(confirmedPassword))//新旧密码是否一致
				return "新密码与确认密码不一致";	
			ui.setPwd(MD5Util.encode2hex(newPassword));	//设置新密码
			userInfoDao.update(ui);//更新密码
			return "密码修改成功";
		}
	//修改邮箱
	public boolean updateEmail(UserInfo userInfo) {
		
		/*UserInfo ui = userInfoDao.get(userInfo.getUserid());
		ui.setEmail(userInfo.getEmail());*/
		userInfo.setEmailValidated("否");
		userInfoDao.update(userInfo);
		return false;
	}
	//发送邮箱
	public boolean sendEmail(UserInfo userInfo) {
		try {
			//获取applicationContext
			ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
			MailSender mailSender = (MailSender)ac.getBean("mailSender");//获取MailSender实例
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			StringBuffer validateString=new StringBuffer("你好!为了完成你的邮箱验证,请点击下链接进行验证 <br><br>");
			validateString.append("若这不是您本人要求的,请忽略本邮件,一切如常.<br><br>")
			.append("<a href=\"http://")
			.append(mailSender.getLocalHost())//服务器IP
			.append(":")
			.append(mailSender.getLocalPort())//服务器运行该项目的服务器的端口号
			.append("/")
			.append(mailSender.getProgectName())//项目名称
			.append("/validateEmail?userid=")
			.append(Base64.encode(DES3.encryptMode(DES3.GetKeyBytes(DES3.getPrivatekey()[DES3.getPrivatekey().length/2]),userInfo.getUserid().getBytes())))//对员工userid进行加密，用于邮箱验证
			.append("&userInfo.email=")
			.append(userInfo.getEmail())//员工邮箱
			.append("&date=")
			.append(sdf.format(new Date()))//发送邮件时时间
			.append("\">确认邮箱验证")
			.append("</a>")
			.append("<br><br><br><br>差旅管理系统");
			

			if(mailSender.send( userInfo.getEmail(), "[差旅管理系统]邮箱验证",validateString.toString()))//发送邮件
				return true;
			else
				return false;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
		
	}
	
	public boolean sendUpdatePWDEmail(UserInfo userInfo) {
		
		try {
			//获取applicationContext
			ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
			MailSender mailSender = (MailSender)ac.getBean("mailSender");//获取MailSender实例
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			StringBuffer validateString=new StringBuffer("您好！,请点击下链接进行密码修改 <br><br>");
			validateString.append("若这不是您本人要求的,请忽略本邮件,一切如常.<br><br>")
			.append("<a href=\"http://")
			.append(mailSender.getLocalHost())//服务器IP
			.append(":")
			.append(mailSender.getLocalPort())//服务器运行该项目的服务器的端口号
			.append("/")
			.append(mailSender.getProgectName())//项目名称
			.append("/validateEmail?userid=")
			.append(Base64.encode(DES3.encryptMode(DES3.GetKeyBytes(DES3.getPrivatekey()[DES3.getPrivatekey().length/2]),userInfo.getUserid().getBytes())))//对员工userid进行加密，用于邮箱验证
			.append("&userInfo.email=")
			.append(userInfo.getEmail())//员工邮箱
			.append("&date=")
			.append(sdf.format(new Date()))//发送邮件时时间
			.append("&type=")
			.append("0")
			.append("\">确认修改密码")
			.append("</a>")
			.append("<br><br><br><br>差旅管理系统");
			if(mailSender.send( userInfo.getEmail(), "[差旅管理系统]修改密码",validateString.toString()))//发送邮件
				return true;
			else
				return false;
		} catch (Exception e) {
			return false;
		}
		
	
	}
	
	public String validateEmail(UserInfo userInfo,String userid,String date){
		Date d;
		try {
			d = new SimpleDateFormat("yyyy-MM-dd").parse(date);
		} catch (ParseException e) {
			
			e.printStackTrace();
			return "验证失败";
		}
		
		if(d.getTime()+ 2*24*3600*1000 < new Date().getTime())//验证时间超过两天则提示过期
			return "日期过期失效";
		 UserInfo ui= userInfoDao.get(userInfo.getUserid());
		if(!userInfo.getEmail().equals(ui.getEmail()))
		{
			return "邮箱不一致";
		}
		if(userid==null||!userid.equals(ui.getUserid()))
		{
			return "员工号不一致";
		}
		else
		{
			if("是".equals(ui.getEmailValidated()))
				return  "邮箱已激活";
			else
			{
				ui.setEmailValidated("是");
				if(!userInfoDao.update(ui))
					return "验证失败";
			}
		}
		return "邮箱验证成功";
		/*Date d;
		try {
			d = new SimpleDateFormat("yyyy-mm-dd").parse(date);
		} catch (ParseException e) {
			
			e.printStackTrace();
			return "验证失败";
		}
		if(d.getTime()+ 2*24*3600*1000 > new Date().getTime())//验证时间超过两天则提示过期
			return "日期过期失效";
		List<UserInfo> userInfoList = userInfoDao.getUserInfoByEmail(userInfo.getEmail());
		
		UserInfo ui = null;
		for(int i=0;i<userInfoList.size();i++)
		{
			if(MD5Util.validate(userInfo.getUserid(), userid))//对员工useid进行加密，然后判断跟之前发送到邮箱的字符串是否一致
			{
				ui = userInfoDao.get(userInfo.getUserid());
				break;
			}
		}
		if(ui != null)
			if("是".equals(ui.getEmailValidated()))
				return  "邮箱已激活";
			else
			{
				ui.setEmailValidated("是");
				if(!userInfoDao.update(ui))
					return "验证失败";
			}
		else 
			return "邮箱不存在";
		return "邮箱验证成功";*/
	}
}
